Tuesday, February 18, 2020

SELECTION SORT - The Sorting Algorithm



 SELECTION SORT

Selection Sort Is Used To Sort The Elements Present In An Array/List. In Selection Sort, An Element Is Compared With All The Elements In The Array/List. So, In One Pass , One Element Of The Array Is Sorted Likewise In Bubble Sorting. Just Like Bubble Sort, Selection Sort Is Also Easy To Understand. Selection Sort Is Often Considered To Be Not So Effecient Type Of Sorting Algorithm Due To Its Complexity.


COMPLEXITIES

Worst complexity: n^2

Average complexity: n^2

Best complexity: n^2

Space complexity: 1


CODE IMPLEMENTATION:


C++

#include<iostream>

using namespace std;

int main()

{

//Declaring Array

int a[] = {4,5,6,3,2,3,6,4,1,0};



//Knowing Size Of Array

int size = (sizeof(a)/sizeof(a[0])) ;



//Bubble Sort Main Function

for(int i=0; i<size; i++)

{

for(int j =i+1; j<size; j++)

{

if(a[i]>a[j])

{

int temp = a[i];

a[i] = a[j];

a[j] = temp;

}

}

}



//Displaying The Sorted Array

for(int i=0; i<size; i++)

{

cout<<a[i];

cout<<endl;

}

}



JAVA

import java.util.*;

import java.io.*;

public class MyClass {

  public static void main(String[] args)

{

  //Declaring array

  int a[] = {4,5,6,3,2,3,6,4,1,0};



  //Storing Size Of Array Into A Variable

  int size = a.length;



  //Bubble Sort Main Function

  for(int i=0; i<size; i++)

   {

     for(int j =i+1; j<size; j++)

      {

       if(a[i]>a[j])

         {

         int temp = a[i];

         a[i] = a[j];

         a[j] = temp;

         }

    }

}



  //Displaying The Sorted Array

  for(int i: a)

    {

      System.out.println(i);

      }

}

}



PYTHON

#declaring Array

a = [4,3,6,2,1,3,0,9,8];


#Getting Size Of List

size = len(a);


#Bubble Sort Main Function

for i in range(0,size):

    for j in range(i+1,size):

        if a[i]>a[j]:

            temp = a[i]

            a[i] = a[j]

            a[j] = temp

            

#Displaying Sorted list

for x in a:

    print(x)


Post a Comment

Whatsapp Button works on Mobile Device only

Start typing and press Enter to search